#include "mythBaseServer.hh"
#include <iostream>
mythBaseServer::mythBaseServer(int port)
	:mythVirtualServer(port)//,mythVirtualSqlite()
{
	//"update server set internalip = '" + ip + "',externalip = '" + ip + "' where serverid = 1"
	//this->open("myth.db");
}

mythBaseServer* mythBaseServer::CreateNew(int port){
	mythBaseServer* baseserver = new mythBaseServer(port);
	return baseserver;
}

int mythBaseServer::UserResult(int argc, char **argv, char **azColName){

	return 0;
}
void mythBaseServer::ServerDecodeCallBack(PEOPLE *people,char* data,int length)
{
	TiXmlDocument *doc = new TiXmlDocument();
	char* tmpdata = data + 29;
	*(data + length - 15) = '\0';
	char mtmp[4096 * 2 + 1] = { 0 };
	mythVirtualSqlite::GetInstance()->convert("GB2312", "UTF-8", mtmp, 4096, tmpdata, strlen(tmpdata));
	if (doc){
		doc->Parse(mtmp);
		TiXmlElement* rootElement = doc->RootElement();  //<xml>
		if (rootElement == NULL)return;
		TiXmlElement* tifuncion = rootElement->FirstChildElement("function");
		//if (tifuncion == NULL)return;
		string tifuncionstr = tifuncion->FirstChild()->Value();
		TiXmlElement* ticontent = rootElement->FirstChildElement("Content");
		//if (ticontent == NULL)return;
		string ticontentstr = ticontent->FirstChild()->Value();
		if (tifuncionstr == "SQL_SELECT"){
			string tmpsql = mythVirtualSqlite::GetInstance()->doSQLex(ticontentstr.c_str());
			people->peopleSendMessage((char*)tmpsql.c_str(), tmpsql.length());
			//free(tmpsql);
		}
		else{
			mythVirtualSqlite::GetInstance()->execSQLex(ticontentstr.c_str());
		}
	}
	delete doc;
	closePeople(people);
	return;
}

mythBaseServer::~mythBaseServer(void)
{
}
